package cn.zwx.common.canal.handler;

import cn.zwx.common.canal.bo.CovertMessageBO;
import cn.zwx.common.canal.constants.CanalConstants;
import cn.zwx.common.canal.enums.BuisType;
import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *  转换参数
 * @author zhangwenxue
 */
@Component
public class CovertMessageHandler {

    private final Logger logger = LoggerFactory.getLogger(CovertMessageHandler.class);


    /**
     * 转换发送消息类
     * @param bo 参数转换BO
     * @return Map<String,Object>
     */
    public Map<String,Object> covertMessageDTO(CovertMessageBO bo) {
        Map<String, Object> returnMap = new LinkedHashMap<>(16);
        // 数据库名称
        returnMap.put("database",bo.getDatabase());
        // 表名称
        returnMap.put("tableName",bo.getTableName());
        // 业务类型 1-新增 2-修改 3-删除
        returnMap.put("busiType",bo.getBusiType());
        // 业务类型描述
        returnMap.put("busiTypeDesc", BuisType.ofValue(bo.getBusiType()));
        // 处理变更之前的数据
        Map<String, String> beforeColumnMap = CollectionUtils.newHashMap(16);
        if (bo.getBusiType().equals(BuisType.UPDATE.getCode()) ||bo.getBusiType().equals(BuisType.DELETE.getCode())){
            List<CanalEntry.Column> beforeColumnsList = bo.getBeforeColumnsList();
            if (!CollectionUtils.isEmpty(beforeColumnsList)){
                for (CanalEntry.Column column : beforeColumnsList) {
                    String columnName = column.getName();
                    String columnValue = column.getValue();
                    beforeColumnMap.put(this.underline2Camel(columnName), columnValue);
                }
            }
        }
        // 处理变更之后的数据
        Map<String, String> afterColumnMap = CollectionUtils.newHashMap(16);
        List<CanalEntry.Column> afterColumnsList = bo.getAfterColumnsList();
        if (!CollectionUtils.isEmpty(afterColumnsList)){
            for (CanalEntry.Column column : afterColumnsList) {
                String columnName = column.getName();
                String columnValue = column.getValue();
                afterColumnMap.put(this.underline2Camel(columnName), columnValue);
            }
        }else {
            logger.info("业务类型:{},无变更之后的数据",BuisType.ofValue(bo.getBusiType()));
        }
        // 组装数据
        if (!CollectionUtils.isEmpty(afterColumnMap)){
            returnMap.put("after",afterColumnMap);
        }
        if (!CollectionUtils.isEmpty(beforeColumnMap)){
            returnMap.put("before",beforeColumnMap);
        }
        logger.info("转换发送消息类转换结果:{}", JSON.toJSONString(returnMap));
        return returnMap;

    }


    /**
     * 下划线转换为驼峰
     * @param line 下划线字符串
     * @param firstIsUpperCase 首字母是否转换为大写
     */
    public String underline2Camel(String line, boolean... firstIsUpperCase) {
        String str = "";
        if(StringUtils.isBlank(line)){
            return str;
        } else {
            StringBuilder sb = new StringBuilder();
            String [] strArr;
            // 不包含下划线，且第二个参数是空的
            if(!line.contains(CanalConstants._Str) && firstIsUpperCase.length == 0){
                sb.append(line.substring(0, 1).toLowerCase()).append(line.substring(1));
                str = sb.toString();
            } else if (!line.contains(CanalConstants._Str) && firstIsUpperCase.length != 0){
                if (!firstIsUpperCase[0]) {
                    sb.append(line.substring(0, 1).toLowerCase()).append(line.substring(1));
                } else {
                    sb.append(line.substring(0, 1).toUpperCase()).append(line.substring(1));
                }
                str = sb.toString();
            } else if (line.contains(CanalConstants._Str) && firstIsUpperCase.length == 0) {
                strArr = line.split(CanalConstants._Str);
                for (String s : strArr) {
                    sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
                }
                str = sb.toString();
                str = str.substring(0, 1).toLowerCase() + str.substring(1);
            } else if (line.contains(CanalConstants._Str) && firstIsUpperCase.length != 0) {
                strArr = line.split(CanalConstants._Str);
                for (String s : strArr) {
                    sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
                }
                if (!firstIsUpperCase[0]) {
                    str = sb.toString();
                    str = str.substring(0, 1).toLowerCase() + str.substring(1);
                } else {
                    str = sb.toString();
                }
            }
        }
        return str;
    }



}
